package API.DateDome.LocalDateTimeDome;

import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Set;

/**
 * ClassName:LocalDateTimeText
 * Package:API.DateDome.LocalDateTimeDome
 * Description:
 *
 * @Author:ZhangXin
 * @Create2023/2/2-12:46
 * @Version:v1.0
 */
public class LocalDateTimeText001 {
    /*
    * ---------JDK8时间类---------
    * Date类
    * 1.ZoneId:时区
    * 2.Instant:时间戳
    * 3.ZoneDateTime：带时区的时间*/
    public static void main(String[] args) {

        /*
        * static Set<String> getAvailableZoneIds()  获取Java中支持的所有时区
        * static ZoneId systemDefault()  获取系统默认时区
        * static ZoneId of(String zoneId) 获取一个指定时区*/

        //获取所有时区名称,数量
        Set<String> zoneIds = ZoneId.getAvailableZoneIds();
        System.out.println(zoneIds.size());//602
//        System.out.println(zoneIds);

        //获取当前系统默认时区
        ZoneId zoneId = ZoneId.systemDefault();
        System.out.println(zoneId);//Asia/Shanghai

        //获取指定的时区
        ZoneId zoneId1 = ZoneId.of("America/Scoresbysund");
        System.out.println(zoneId1);//America/Scoresbysund

        //获取当前时间的Instant对象(标准时间)
        Instant now = Instant.now();
        System.out.println(now);//2023-02-02T05:07:14.953897500Z

        //根据(秒/毫秒/纳秒)获取Instant对象
        Instant instant = Instant.ofEpochMilli(0L);
        System.out.println(instant);//1970-01-01T00:00:00Z

        Instant instant1 = Instant.ofEpochSecond(1);
        System.out.println(instant1);//1970-01-01T00:00:01Z

        Instant instant2 = Instant.ofEpochSecond(1L, 1000000000L);
        System.out.println(instant2);//1970-01-01T00:00:02Z

        //指定时区
        ZonedDateTime zonedDateTime = Instant.now().atZone(ZoneId.of("Asia/Shanghai"));
        System.out.println(zonedDateTime);//2023-02-02T13:12:45.219925+08:00[Asia/Shanghai]

        //isXxx 判断
        Instant instant3 = Instant.ofEpochMilli(0L);
        Instant instant4 = Instant.ofEpochMilli(1000L);
        //判断调用者代表的时间是否在参数代表时间的后面
        boolean result1 = instant4.isAfter(instant3);//true
        //判断调用者代表的时间是否在参数代表时间的前面
        boolean result2 = instant4.isBefore(instant3);//false
        System.out.println(result1);
        System.out.println(result2);


        //减少时间
        Instant instant5 = Instant.ofEpochMilli(3000L);
        System.out.println(instant5);//1970-01-01T00:00:03Z
        Instant seconds = instant5.minusSeconds(1);
        System.out.println(seconds);//1970-01-01T00:00:02Z

        //获取当前时间对象(带时区)
        ZonedDateTime now1= ZonedDateTime.now();
        System.out.println(now1);//2023-02-02T13:22:11.874566800+08:00[Asia/Shanghai]
        //获取指定的时间对象(带时区
        // 年月日时分秒纳秒方式指定
        ZonedDateTime time1 = ZonedDateTime.of(2023,2,2,13,
                25,20,0,ZoneId.of("Asia/Shanghai"));
        System.out.println(time1);//2023-02-02T13:25:20+08:00[Asia/Shanghai]

        //获取Instant + 时区的方式指定获取时间对象
        Instant instant6 = Instant.ofEpochMilli(0L);
        ZoneId zoneId2 =ZoneId.of("Asia/Shanghai");
        ZonedDateTime time2 = ZonedDateTime.ofInstant(instant6,zoneId2);
        System.out.println(time2);//1970-01-01T08:00+08:00[Asia/Shanghai]

        //withXxx 修改时间系列方法
        ZonedDateTime time3 = time2.withYear(2000);
        System.out.println(time3);//2000-01-01T08:00+08:00[Asia/Shanghai]

        //减少时间
        ZonedDateTime time4 = time3.minusYears(3);
        System.out.println(time4);//1997-01-01T08:00+08:00[Asia/Shanghai]

        //增加时间
        ZonedDateTime time5 = time4.plusYears(2);
        System.out.println(time5);//1999-01-01T08:00+08:00[Asia/Shanghai]


        /*
        * JDK8新增的时间对象都是不可变得
        * 如果我们修改了,减少了,增加了时间
        * 那么调用者是不会发生改变的,产生一个新的时间*/


    }
}
